const mysql = require('mysql2/promise');
const config = require('./config.json');

const pool = mysql.createPool(config.db);

module.exports = {
  // 查询单个用户
  async getUser(username) {
    const conn = await pool.getConnection();
    try {
      const [rows] = await conn.query('SELECT * FROM userinfo WHERE username = ?', [username]);
      return rows[0];
    } finally {
      conn.release();
    }
  },

  // 创建用户
  async createUser(username, password, role, id) {
    const conn = await pool.getConnection();
    try {
      await conn.query('INSERT INTO userinfo (username, password, role, id) VALUES (?, ?, ?, ?)', [username, password, role, id]);
    } finally {
      conn.release();
    }
  }
};
